<?php

/*
 * litchicms
 * Copyright (C) ${date} Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");

$conf = array(
    'serial' => 25,
    'name' => 'category',
    'title' => '分类',
    'desc' => '如果要对数据集分类,请启用此属性',
    'ui'=>TRUE,
    'global'=>TRUE,
    'group' => array(1, 'content'),
    'buildUiParam'=>TRUE,
    'buildField'=>TRUE,
    'columns' => array(
        'category' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => "''"),
        'category_cache' => array('type' => 'text', 'not null' => TRUE, 'default' => "''"),
    ),
    'columnsDescription' => array(
        'category' => '所属分类id, 形如 5,4,8',
        'category_cache' => '序列化后的所属分类信息'
    ),
    'show' => array(
        'listhead' => array(
            'serial' => 20, 'width' => '8%', 'text' => '所属分类'
        ),
        'listbody' => array(
            'serial' => 20, 'text' => '<div class="data_one_row fsize10">($category=$COLL_row["category_cache"]|unserialize){foreach from=$category item="cat"}<a href="?{$cat["cid"]|bgpre:"sh-category/cid-".this}">{$cat["cname"]}</a><br/>{/foreach}</div>', 'align' => 'left'
        ),
        'bottom' => array(
            'serial' => 5, 'text' => '更改分类', 'onclick' => 'id_check_box_changecategory()',
        ),
    )
);

$GLOBALS['BRK_GLOBAL']['ComponentEnabledProcessor'][$conf['name']] = 'ComponentEnabledProcessor_category';
$GLOBALS['BRK_GLOBAL']['ComponentDisableProcessor'][$conf['name']] = 'ComponentDisableProcessor_category';

!defined('CATEGORY_right_pid') && define('CATEGORY_right_pid', 86);

if (!function_exists('ComponentEnabledProcessor_category')) {

    function ComponentEnabledProcessor_category($collection) {
        if (!$temp = brk_select_one('right', '*', "WHERE right_pid=" . CATEGORY_right_pid . " AND right_node='" . $collection['collection_ename'] . "'")) {
            $dataarr = array(
                'right_pid' => CATEGORY_right_pid,
                'right_node' => $collection['collection_ename'],
                'right_note' => $collection['collection_cname'],
            );
            if (brk_insert('right', $dataarr)) {
                
            } else {
                exit("ComponentEnabledProcessor_category brk_insert right");
            }
        }

//    dump($collection);

        if (!$temp = brk_select_one('category', "*", "WHERE lid=1 AND cname='" . addslashes($collection['collection_cname']) . "' AND  ctype='" . addslashes($collection['collection_ename']) . "' ")) {
            $dataarr = array(
                'lid' => 1,
                'rid' => 2,
                'cname' => $collection['collection_cname'],
                'ctype' => $collection['collection_ename'],
                'cnote' => $collection['collection_desc']
            );
            //初始化分类.
            $CATEGORY_ID = brk_insert('category', $dataarr);
        } else {
            $CATEGORY_ID = $temp['cid'];
        }

        $GLOBALS['ComponentVar'][]="\$CATEGORY_ID=$CATEGORY_ID;";

        $code = "<?php !defined('BRK') && exit('!');
\$CATEGORY_ID=$CATEGORY_ID;
\$COLLECTION_ID=$collection[collection_id];
\$COLLECTION_ENAME=\"" . replace_yy($collection['collection_ename']) . "\";
\$COLLECTION_CNAME=\"" . replace_yy($collection['collection_cname']) . "\";
intval(\$COLLECTION_ID)==0 && exit('\$COLLECTION_ID=0??');
\$COLLECTION_DESC=\"" . replace_yy($collection['collection_desc']) . "\";
require dirname(__FILE__) . '/../category.php';\n
";
        check_make_dir(SYSTEM . 'litchi/content/category');
        file_put_contents(SYSTEM . 'litchi/content/category/' . $collection['collection_ename'] . '.php', $code);

        check_make_dir(TPL_DIR . 'sys/content/category');
        $tpl = "<?php exit(\"breakcms\"); ?>\n{include file=\"sys/content/category.php\"}";
        file_put_contents(TPL_DIR . 'sys/content/category/' . $collection['collection_ename'] . '.php', $tpl);

//    dump($collection);
    }

}

if (!function_exists('ComponentDisableProcessor_category')) {

    function ComponentDisableProcessor_category($collection) {
        brk_delete('right', "WHERE right_pid=" . CATEGORY_right_pid . " AND right_node='" . $collection['collection_ename'] . "'");

        if ($temp = brk_select_one('category', "*", "WHERE lid=1 AND cname='" . addslashes($collection['collection_cname']) . "' AND  ctype='" . addslashes($collection['collection_ename']) . "' ")) {
            //加入 删除(不可恢复) 回收站(可恢复)
        }

        //删除权限
        brk_delete('right', "WHERE right_pid=" . CATEGORY_right_pid . " AND right_node='" . $collection['collection_ename'] . "'");

        unlink(SYSTEM . 'litchi/content/category/' . $collection['collection_ename'] . '.php');
        unlink(TPL_DIR . 'sys/content/category/' . $collection['collection_ename'] . '.php');
    }

}